package com.example.travel.fragment;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.travel.Interface.Implements.UserInterfaceImpl;
import com.example.travel.R;
import com.example.travel.bean.User;


public class AddContactFragment extends Fragment {
    EditText etAddUserName,etAddPhone;
    Button btAddUser,btAddCancel;
    UserInterfaceImpl uii;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.add_contact, container, false);
        inint(view);
        btAddUser.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                String userName=etAddUserName.getText().toString().trim();
                String phone=etAddPhone.getText().toString().trim();
                if(userName==null||userName.equals("")||phone==null||phone.equals("")){
                    Toast.makeText(getContext(),"请输入动态!",Toast.LENGTH_SHORT).show();
                }else{
                    //把数据封装到User对象
                    User user=new User();
                    user.setUser_name(userName);
                    user.setPhone(phone);
                    //把user对象通过业务逻辑层保存到数据库contact的表user_tabel中
                    uii=new UserInterfaceImpl(getContext());
                    uii.insertUser(user);
                    Toast.makeText(getContext(),"动态保存成功!",Toast.LENGTH_SHORT).show();
                    FragmentManager fragmentManager=getFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.flconent,new MyContactFragment()).commit();
                }
            }
        });

        return view;
    }
    public void inint(View view){
        etAddPhone=(EditText)view.findViewById(R.id.et_addphone);
        etAddUserName=(EditText)view.findViewById(R.id.et_addUserName);
        btAddCancel=(Button)view.findViewById(R.id.bt_addCancel);
        btAddUser=(Button)view.findViewById(R.id.bt_addUser);
    }

}
